package com.example.utils;


import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class CompressionUtils {

    public static String compress(String text) {
        if (text == null || text.isEmpty()) return text;
        try {
            byte[] input = text.getBytes(StandardCharsets.UTF_8);
            Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
            deflater.setInput(input);
            deflater.finish();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            while (!deflater.finished()) {
                int compressedLength = deflater.deflate(buffer);
                outputStream.write(buffer, 0, compressedLength);
            }
            deflater.end();

            byte[] compressed = outputStream.toByteArray();
            return Base64.getEncoder().encodeToString(compressed);
        } catch (Exception e) {
            throw new RuntimeException("压缩失败", e);
        }
    }

    public static String decompress(String compressedText) {
        if (compressedText == null || compressedText.isEmpty()) return compressedText;
        try {
            byte[] compressed = Base64.getDecoder().decode(compressedText);
            Inflater inflater = new Inflater();
            inflater.setInput(compressed);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            while (!inflater.finished()) {
                int decompressedLength = inflater.inflate(buffer);
                outputStream.write(buffer, 0, decompressedLength);
            }
            inflater.end();

            return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException("解压失败", e);
        }
    }

    public static void main(String[] args) throws Exception {
        var text = """
            {"cells":[{"shape":"edge","attrs":{"line":{"stroke":"#A6A6A6","targetMarker":{"name":"classic","size":8}}},"id":"50969de4-5d2d-4548-96e3-fe3316757997","defaultLabel":{"markup":[{"tagName":"ellipse","selector":"bg"},{"tagName":"text","selector":"txt"}],"attrs":{"txt":{"fill":"#FFFFFF","textAnchor":"middle","textVerticalAnchor":"middle"},"bg":{"ref":"txt","refRx":"50%","refRy":"50%","stroke":"#7c68fc","fill":"#7c68fc","strokeWidth":1}},"zIndex":1},"router":{"name":"er","args":{"offset":"center"}},"labels":null,"zIndex":-1,"data":{"source":"cdd9534e-8877-4543-a94a-0377a2eb4e6c","target":"d35bc44d-e988-49f7-82e1-6648d6134f54"},"source":{"cell":"cdd9534e-8877-4543-a94a-0377a2eb4e6c","port":"cdd9534e-8877-4543-a94a-0377a2eb4e6c"},"target":{"cell":"d35bc44d-e988-49f7-82e1-6648d6134f54","port":"d35bc44d-e988-49f7-82e1-6648d6134f54"}},{"shape":"edge","attrs":{"line":{"stroke":"#A6A6A6","targetMarker":{"name":"classic","size":8}}},"id":"a9945617-4799-46bf-8177-42c4273af2ad","defaultLabel":{"markup":[{"tagName":"ellipse","selector":"bg"},{"tagName":"text","selector":"txt"}],"attrs":{"txt":{"fill":"#FFFFFF","textAnchor":"middle","textVerticalAnchor":"middle"},"bg":{"ref":"txt","refRx":"50%","refRy":"50%","stroke":"#7c68fc","fill":"#7c68fc","strokeWidth":1}},"zIndex":1},"router":{"name":"er","args":{"offset":"center"}},"labels":null,"zIndex":-1,"data":{"source":"d35bc44d-e988-49f7-82e1-6648d6134f54","target":"ca95de3b-f09f-47c9-86de-754e5a174ca7"},"source":{"cell":"d35bc44d-e988-49f7-82e1-6648d6134f54","port":"d35bc44d-e988-49f7-82e1-6648d6134f54"},"target":{"cell":"ca95de3b-f09f-47c9-86de-754e5a174ca7","port":"ca95de3b-f09f-47c9-86de-754e5a174ca7"},"tools":{"items":[],"name":null}},{"position":{"x":-310,"y":-280},"size":{"width":150,"height":30},"view":"vue-shape-view","shape":"custom-vue-node","id":"cdd9534e-8877-4543-a94a-0377a2eb4e6c","data":{"type":"RuleNodes","name":"22","name2":"规则节点","component":{"name":"CreditCard","__name":"credit-card"},"position":"graph","showTool":false,"dataTemp":{"id":1177,"nodeId":"cdd9534e-8877-4543-a94a-0377a2eb4e6c","appCode":"B008","validatorScenes":"test_1","validatorName":"22","priority":0,"validatorType":"VALIDATOR","validatorCategory":"02","control":"WEEK","exitMechanism":"CURRENT_LINE","errorMsg":"22222","admitRuleId":1518,"admitRuleName":"admin规则2222","validateRuleId":1517,"validateRuleName":"规则2","valueExpression":"222222","description":"22","admitRuleScriptContent":"11111111111","validateRuleScriptContent":"33333333","admitRuleScriptType":"Groovy","validateRuleScriptType":"Groovy","category":"06","status":1,"createTime":"2025-05-16 18:04:07","updateTime":"2025-05-16 18:04:07","validatorSceneNames":"测试1"}},"ports":{"groups":{"right":{"position":{"name":"right"},"width":20,"height":30,"markup":[{"tagName":"rect","selector":"rect"},{"tagName":"circle","selector":"circle"}],"attrs":{"rect":{"magnet":true,"stroke":"rgba(255,255,255,0)","fill":"rgba(255,255,255,0)","strokeWidth":1,"width":30,"height":30,"x":-15,"y":-15,"style":{"visibility":"hidden"}},"circle":{"magnet":true,"stroke":"#5F95FF","r":5,"strokeWidth":3,"fill":"#fff","style":{"visibility":"hidden"}}},"zIndex":99}},"items":[{"group":"right","id":"eb3caadd-b400-421c-96af-ea06b3ad8ced"}]},"zIndex":1},{"position":{"x":-60,"y":-280},"size":{"width":150,"height":30},"view":"vue-shape-view","shape":"custom-vue-node","id":"d35bc44d-e988-49f7-82e1-6648d6134f54","data":{"name":"11","type":"RuleNodes","name2":"规则节点","component":{"name":"CreditCard","__name":"credit-card"},"showTool":false,"dataTemp":{"id":1109,"nodeId":"d35bc44d-e988-49f7-82e1-6648d6134f54","appCode":"B008","validatorScenes":"test_1","validatorName":"11","priority":0,"validatorType":"PROCESS","validatorCategory":"02","control":null,"exitMechanism":"CURRENT_TASK","errorMsg":null,"admitRuleId":null,"admitRuleName":null,"validateRuleId":1432,"validateRuleName":"规则1","valueExpression":null,"description":"11","admitRuleScriptContent":null,"validateRuleScriptContent":"111","admitRuleScriptType":null,"validateRuleScriptType":"Groovy","category":null,"status":1,"createTime":"2025-05-15 10:14:12","updateTime":"2025-05-15 10:14:12","validatorSceneNames":"test11,测试1"}},"ports":{"groups":{"right":{"position":{"name":"right"},"width":20,"height":30,"markup":[{"tagName":"rect","selector":"rect"},{"tagName":"circle","selector":"circle"}],"attrs":{"rect":{"magnet":true,"stroke":"rgba(255,255,255,0)","fill":"rgba(255,255,255,0)","strokeWidth":1,"width":30,"height":30,"x":-15,"y":-15,"style":{"visibility":"hidden"}},"circle":{"magnet":true,"stroke":"#5F95FF","r":5,"strokeWidth":3,"fill":"#fff","style":{"visibility":"hidden"}}},"zIndex":99}},"items":[{"group":"right","id":"2630be99-562c-4e88-9796-11b4d9f79cb3"}]},"zIndex":2},{"position":{"x":190,"y":-280},"size":{"width":150,"height":30},"view":"vue-shape-view","shape":"custom-vue-node","id":"ca95de3b-f09f-47c9-86de-754e5a174ca7","data":{"name":"数据校验","type":"RuleNodes","name2":"规则节点","component":{"name":"CreditCard","__name":"credit-card"},"showTool":true,"dataTemp":{"id":1073,"nodeId":"ca95de3b-f09f-47c9-86de-754e5a174ca7","appCode":"B008","validatorScenes":"test_1","validatorName":"数据校验","priority":0,"validatorType":"VALIDATOR","validatorCategory":"02","control":"WEEK","exitMechanism":"NOT","errorMsg":"错误提示","admitRuleId":null,"admitRuleName":null,"validateRuleId":1363,"validateRuleName":"测试","valueExpression":"","description":"","admitRuleScriptContent":null,"validateRuleScriptContent":"sss","admitRuleScriptType":null,"validateRuleScriptType":"Groovy","category":"06","status":1,"createTime":"2025-05-08 16:33:10","updateTime":"2025-05-12 18:01:55","validatorSceneNames":"测试1"}},"ports":{"groups":{"right":{"position":{"name":"right"},"width":20,"height":30,"markup":[{"tagName":"rect","selector":"rect"},{"tagName":"circle","selector":"circle"}],"attrs":{"rect":{"magnet":true,"stroke":"rgba(255,255,255,0)","fill":"rgba(255,255,255,0)","strokeWidth":1,"width":30,"height":30,"x":-15,"y":-15,"style":{"visibility":"hidden"}},"circle":{"magnet":true,"stroke":"#5F95FF","r":5,"strokeWidth":3,"fill":"#fff","style":{"visibility":"hidden"}}},"zIndex":99}},"items":[{"group":"right","id":"203316e3-bfcc-43f0-a8bf-d7559a1f26bc"}]},"zIndex":3}]}
            """;
        var compress = compress(text);
        System.out.println(compress);

        System.out.println(decompress(compress));


    }
}